<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/10
 * Time: 17:11
 */
namespace app\pay\controller;
use think\Controller;
use think\Loader;
use think\Config;
class Alipay extends Controller
{
    public function pay()
    {
        Loader::import('alipay.f2fpay.model.builder.AlipayTradePrecreateContentBuilder', EXTEND_PATH);
        Loader::import('alipay.f2fpay.service.AlipayTradeService', EXTEND_PATH);
        $outTradeNo = "Ali".date('Ymdhis').mt_rand(100,1000);
        $post['subject'] = '充值';
        $totalAmount = 1;
        $config = array (
            //签名方式,默认为RSA2(RSA2048)
            'sign_type' => "RSA2",
            //支付宝公钥
            'alipay_public_key' => "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAt0/4j+at+2JGQboOUqeiDV9R8IVDrev4K+OY4BWCbRO8JWAPYCm0SjJ/ML3o+4AsF2DX5m6n3aIVmN+H/qT0zKifsmHURh+QAJkicUXHSL9Gx8z1GqQiWb9JFaoca2m9sKfNh/jtHdX2d1TYqGXfmIKWrQXJZGhsUAaYsKCKYGjY1umkYqHHHq8mh3d5aeW8lI97vbtMFXRdynCP9Xzfuq1poCRLCku92jMDfgOnkFTseOZdzIrkbCHipDyKbGd/sKQMiv98ybCzb1uiFLRCp1oU/SfAL9fY2jtLQro9oOwCe0X7ZFv7h29jIlWNMF6OgI8BXzEKoShoOnvBilZpEQIDAQAB",

            //商户私钥
            'merchant_private_key' => "MIIEpAIBAAKCAQEA0HPfYEi8j29CcdtxM4mgqXHaCzHr5UopJtfnpj9lTPuWR7nRg3BVZzc9PS2XvcQHIU+PvqEbsQOuazr/f/i58hQk4Ry2EXU9MkWAzfvXjYmZ15L1Ie7L43fc+ldKmqgQY4FhfsCliBWnFoX0CQPX6MIp5HiEEdcbsSJH5cp9eJpW30CBbM5a8JalHCzWFUgkfI7aQSS/vORdL8fO6v+3D1qUzVMHZcmneOXUxjrFz7Z7JOArh8v358uJ92LJRHcZFQmnDht9S3smOKuR6mSFM/uBzeHBCPDFkoUy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MDyKMcCS+3xUB41JfTsU/GXJzTwVFvpvoQKBgQCsr4AyFG2eA2yDxc+SroFKTSR2Nd1hM7srQRlvHhrp2Z1Xh6Ir9V9qNFXMGoDBtJZdyWO5ljysn1HwNaqDzdVWmG4VCT2YdTRKbyduOmLwzD7pVN8PVj9EkbQeie0Xe+xBCVuBvDTkBX9HSm41j3Jqc4ogkxDXcVqeH1wWw88hpQ==",

            //编码格式
            'charset' => "UTF-8",

            //支付宝网关
            'gatewayUrl' => "https://openapi.alipaydev.com/gateway.do",

            //应用ID
            'app_id' => "2016091300499323",

            //异步通知地址,只有扫码支付预下单可用
            'notify_url' => "http://www.baidu.com",

            //最大查询重试次数
            'MaxQueryRetry' => "10",

            //查询间隔
            'QueryDuration' => "3"
        );
        //创建请求builder，设置请求参数
        $qrPayRequestBuilder = new \AlipayTradePrecreateContentBuilder();
        $qrPayRequestBuilder->setOutTradeNo($outTradeNo);//订单编号
        $qrPayRequestBuilder->setTotalAmount($totalAmount);//订单金额
        $qrPayRequestBuilder->setsubject('商品标题');//商品标题

         //调用qrPay方法获取当面付应答
        $qrPay = new \AlipayTradeService($config);
        $qrPayResult = $qrPay->qrPay($qrPayRequestBuilder);

        // 根据状态值进行业务处理
        switch ($qrPayResult->getTradeStatus()) {
            case "SUCCESS":
                echo "支付宝支付成功:" . "<br>--------------------------<br>";
                print_r($qrPayResult->getResponse());
                break;
            case "FAILED":
                echo "支付宝支付失败!!!" . "<br>--------------------------<br>";
                if (!empty($qrPayResult->getResponse())) {
                    print_r($qrPayResult->getResponse());
                }
                break;
            case "UNKNOWN":
                echo "系统异常，订单状态未知!!!" . "<br>--------------------------<br>";
                if (!empty($qrPayResult->getResponse())) {
                    print_r($qrPayResult->getResponse());
                }
                break;
            default:
                echo "不支持的交易状态，交易返回异常!!!";
                break;
        }


    }

}